// CyTargetLinker,
// a Cytoscape plugin to extend biological networks with regulatory interaction
// Copyright 2011-2012 BiGCaT Bioinformatics
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package cytargetlinker;

import java.io.File;
import java.util.Map;

import cytoscape.Cytoscape;
import cytoscape.layout.CyLayoutAlgorithm;
import cytoscape.layout.CyLayouts;
import cytoscape.task.Task;
import cytoscape.task.TaskMonitor;

/**
 * Cytoscape Task - progress bar while
 * the user network is extended
 *
 */
public class MergeTask implements Task {
		private TaskMonitor taskMonitor;
		
		private CyTargetLinker linker;
		private Map<String, File> rins;
		
		public MergeTask(CyTargetLinker linker, Map<String, File> rins) {
			this.linker = linker;
			this.rins = rins;
		}

		public void setTaskMonitor(TaskMonitor monitor) throws IllegalThreadStateException {
			taskMonitor = monitor;
		}

		public void halt() {
			// not implemented
		}

		public String getTitle() {
			return "Load Regulatory Interaction Networks ...";
		}

		public void run() {
			taskMonitor.setStatus("Loading...");
			taskMonitor.setPercentCompleted(-1);
			linker.loadNetworks(rins);
			linker.mergeNetworks();
			linker.createColorTab();
			
			String status = "The following RINs have been loaded\n\n";
			for(RegulatoryInteractionNetwork net : linker.getCurrentObj().getRINs()) {
				status = status + "> " + net.getName() + " | " + net.getNewInteractions() + " regulatory interactions\n";
			}
			Cytoscape.getNetworkView(linker.getResNetName()).redrawGraph(true, false);
			CyLayoutAlgorithm alg = CyLayouts.getLayout("force-directed");
			alg.doLayout();
			taskMonitor.setStatus(status);
			taskMonitor.setPercentCompleted(100);
		}
	}